package com.cwj.we.module.bizhi;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.app.ActivityCompat;
import androidx.viewpager.widget.ViewPager;

import com.cwj.we.R;
import com.cwj.we.base.BaseActivity;
import com.cwj.we.base.BasePresenter;
import com.cwj.we.bean.BiZhiBean;
import com.cwj.we.utils.ImgDonwload;
import com.cwj.we.utils.OneClickThree;
import com.cwj.we.utils.ToastUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gyf.immersionbar.ImmersionBar;

import java.lang.reflect.Type;
import java.util.List;

import butterknife.BindView;

/**
 * 图片预览
 */
public class PhotoView2Activity extends BaseActivity {

    @BindView(R.id.view_page)
    ViewPager viewPage;
    @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.cl_view)
    CoordinatorLayout clView;
    @BindView(R.id.tvDowonload)
    TextView tvDowonload;
    @BindView(R.id.tvShare)
    TextView tvShare;

    private MyImage2Adapter adapter;
    private List<BiZhiBean.ResBean.VerticalBean> imgList;
    private int currentPosition;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE"};
    private static final int REQUEST_EXTERNAL_STORAGE = 1;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_photo_view2;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void initData() {
        Intent intent = getIntent();
        currentPosition = intent.getIntExtra("currentPosition", 0);
        Type listType = new TypeToken<List<BiZhiBean.ResBean.VerticalBean>>() {
        }.getType();
        imgList = new Gson().fromJson(intent.getStringExtra("dataBean"), listType);

        tv.setText((currentPosition + 1) + "/" + imgList.size());

        adapter = new MyImage2Adapter(imgList, this);
        viewPage.setAdapter(adapter);
        viewPage.setCurrentItem(currentPosition, false);
        viewPage.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                currentPosition = position;
                tv.setText((currentPosition + 1) + "/" + imgList.size());
            }
        });

        //下载
        tvDowonload.setOnClickListener(view -> {
            if (!OneClickThree.isFastClick()) {
                //检测是否有写的权限
                if (ActivityCompat.checkSelfPermission(PhotoView2Activity.this,
                        "android.permission.WRITE_EXTERNAL_STORAGE") != PackageManager.PERMISSION_GRANTED) {
                    // 没有写的权限，去申请写的权限，会弹出对话框
                    ActivityCompat.requestPermissions(PhotoView2Activity.this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
                } else {
                    ImgDonwload.donwloadImg(PhotoView2Activity.this, imgList.get(currentPosition).getImg(), String.valueOf(System.nanoTime()));
                }
            } else {
                ToastUtil.showTextToast(PhotoView2Activity.this, "请勿重复点击");
            }
        });

        //分享
        tvShare.setOnClickListener(view -> {
            if (!OneClickThree.isFastClick()) {
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, imgList.get(currentPosition).getImg());//注意：这里只是分享文本内容
                sendIntent.setType("text/*");
                startActivity(Intent.createChooser(sendIntent, "分享到"));
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_EXTERNAL_STORAGE && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            ImgDonwload.donwloadImg(PhotoView2Activity.this, imgList.get(currentPosition).getImg(), String.valueOf(System.nanoTime()));
        } else {
            ToastUtil.showTextToast(PhotoView2Activity.this, "同意权限后方能下载");
        }
    }

    @Override
    public void initView() {
        ImmersionBar.with(this)
                .init();
    }
}
